package com.sit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sit.entity.Ca;
import com.sit.entity.CompetitionA;
import com.sit.mapper.CompetitionAMapper;
import com.sit.service.CompetitionAService;
import com.sit.vo.CaVO;
import com.sit.vo.CompetitionAVO;
import com.sit.vo.DataVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CompetitionAServiceImpl implements CompetitionAService {

    @Autowired
    private CompetitionAMapper competitionAMapper;
    @Override
    public DataVO<CompetitionAVO> findData() {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");
        dataVO.setCount(competitionAMapper.selectCount(null).longValue());
        List<CompetitionA> competitionAList = competitionAMapper.selectList(null);
        dataVO.setData(competitionAList);
        return dataVO;
    }

    @Override
    public boolean addComp(CompetitionA competitionA) {
        CompetitionA competitionA1 = new CompetitionA();
        competitionA1.setCnA(competitionA.getCnA());
        competitionA1.setCnoA(competitionA.getCnoA());
        competitionA1.setMax(competitionA.getMax());
        competitionA1.setPlaceA(competitionA.getPlaceA());
        competitionA1.setDateA(competitionA.getDateA());
        int flag1 =competitionAMapper.insert(competitionA1);

        if(flag1 >0){
            return  true;
        }else {return false;}
    }

    @Override
    public boolean deleteComp(String cnoA) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("cno_a",cnoA);
        int flag =competitionAMapper.delete(wrapper);
        if(flag >0){
            return  true;
        }else {return false;}
    }

    @Override
    public boolean updateComp(CompetitionA competitionA) {
        CompetitionA competitionA1 = new CompetitionA();
        BeanUtils.copyProperties(competitionA,competitionA1);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("cno_a",competitionA.getCnoA());
        int flag =competitionAMapper.update(competitionA1,wrapper);
        if(flag >0){
            return  true;
        }else {return false;}
    }

    @Override
    public DataVO<CompetitionAVO> searchComp(String cnA) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("cn_a",cnA);
        dataVO.setCount(competitionAMapper.selectCount(wrapper).longValue());

        CompetitionAVO competitionAVO = new CompetitionAVO();
        BeanUtils.copyProperties(competitionAMapper.selectOne(wrapper),competitionAVO);
        List<CompetitionAVO> competitionAVOList = new ArrayList<>();
        competitionAVOList.add(competitionAVO);
        dataVO.setData(competitionAVOList);
        return dataVO;
    }
}
